package com.parktech.sun.domain.service.quartzTask;

import com.parktech.sun.domain.model.Constants;
import com.parktech.sun.domain.model.email.Email;
import com.parktech.sun.domain.model.sms.Sms;
import com.parktech.sun.domain.service.sunhz.EmailService;
import com.parktech.sun.domain.service.sunhz.SmsService;
import com.parktech.sun.utils.action.MailSender;
import com.parktech.sun.utils.action.SmsProcessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.security.pkcs11.wrapper.CK_DATE;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: guo
 * Date: 11-1-16
 * Time: 下午2:29
 * To change this template use File | Settings | File Templates.
 */
@Service("actionSendService")
public class ActionSendServiceImpl {
    @Autowired
    private EmailService emailService;

    @Autowired
    private SmsService smsService;

    /**
     * send the quartz task of email
     */
    public void sendEmailTask() {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String compareDatetimeStr = dateFormat.format(new Date());
        List fieldColumns = new ArrayList();
        List searchValues = new ArrayList();
        fieldColumns.add("status");
        searchValues.add(Constants.ACTION_STATUS_SCHEDULED);
        String[] compareColumns = {"scheduleSendDatetime"};
        String[] compareDirections = {"le"};
        Object[] compareValues = {new Date()};
        List<Email> emailList = emailService.getPaginationObjectsWithSimilarConditionContainCompare(new Email(), 1, 6666, fieldColumns, searchValues, compareColumns, compareDirections, compareValues, null, null);
        if (emailList.size() != 0) {
            MailSender mailSender = new MailSender();
            for (Email email : emailList) {
                boolean isSuccess =  mailSender.send(email.getAcceptorEmail(), email.getTopic(), email.getContent(),email.getSenderName(),email.getSenderPassword());
                if (isSuccess && email.isSaveHistory()) {
                    email.setStatus(Constants.ACTION_STATUS_SUCCESS);
                    emailService.saveOrUpdateBean(email);
                }
                else if(isSuccess && !email.isSaveHistory())
                {
                    emailService.removeBean(email.getId());
                }
                else if(!isSuccess)
                {
                    email.setStatus(Constants.ACTION_STATUS_FAILED);
                    emailService.saveOrUpdateBean(email);
                }
            }
        }
    }

    /**
     * schedule to send the task of sms.
     */
    public void sendSmsTask() {
     //   DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      //  String compareDatetimeStr = dateFormat.parse(new Date());
       // Calendar.getInstance().
        List fieldColumns = new ArrayList();
        List searchValues = new ArrayList();
        fieldColumns.add("status");
        searchValues.add(Constants.ACTION_STATUS_SCHEDULED);
        String[] compareColumns = {"scheduleSendDatetime"};
        String[] compareDirections = {"le"};
        Object[] compareValues = {new Date()};
        List<Sms> smsList = smsService.getPaginationObjectsWithSimilarConditionContainCompare(new Sms(), 1, 6666, fieldColumns, searchValues, compareColumns, compareDirections, compareValues, null, null);
        if (smsList.size() != 0) {
            SmsProcessor smsProcessor = new SmsProcessor();
            for (Sms sms : smsList) {
                String retMsg = smsProcessor.send(sms.getContent(), sms.getAcceptorSms().trim());
                if (retMsg.equals("success")) {
                    sms.setStatus(Constants.ACTION_STATUS_SUCCESS);
                } else {
                    sms.setStatus(Constants.ACTION_STATUS_FAILED);
                }
                if (sms.isSaveHistory()) {
                    smsService.saveOrUpdateBean(sms);
                }
                else if(retMsg.equals("success"))
                {
                    smsService.removeBean(sms.getId());
                }
            }
        }
    }
}
